
package com.axc.gallery2.view;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.axc.gallery2.R;
import com.axc.gallery2.dbhelper.NetAlbumInfo;
import com.axc.gallery2.view.ImageAdapter.ViewHolder;

public class NetAlbumGridViewAdapter extends BaseAdapter {

    private Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    private List<NetAlbumInfo> _netAlbumInfos;

    public void set_netAlbumInfos(List<NetAlbumInfo> netAlbumInfos) {
        _netAlbumInfos = netAlbumInfos;
    }

    public NetAlbumGridViewAdapter(Context context, List<NetAlbumInfo> netAlbumInfos) {

        System.out.println("size :" + netAlbumInfos.size());
        this.context = context;
        _netAlbumInfos = netAlbumInfos;
    }

    public List<NetAlbumInfo> getnetAlbumInfos() {
        return _netAlbumInfos;
    }

    public void clearConvertView() {
        convertviewList.clear();
    }

    // 取得图片
    List<SoftReference<View>> convertviewList = new ArrayList<SoftReference<View>>();
    @SuppressWarnings("finally")
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("NetGridViewAdapter1");
        ViewHolder viewHolder;
        try {
            convertView = convertviewList.get(position).get();
        } catch (IndexOutOfBoundsException e) {
            System.out.println("NetGridViewAdapter2");
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.net_ablums_folder_item, null);
            convertView.setTag(viewHolder);
            viewHolder.nameTextView = (TextView) convertView
                    .findViewById(R.id.net_ablums_folder_title);
            viewHolder.nameTextView.setText(_netAlbumInfos.get(position).getNetAlbumName() + "("
                    + _netAlbumInfos.get(position).getSize() + ")");
            viewHolder.idTextView = (TextView) convertView.findViewById(R.id.net_ablums_folder_aid);
            viewHolder.idTextView.setText(_netAlbumInfos.get(position).getAID());
            convertviewList.add(new SoftReference<View>(convertView));
            System.out.println("NetGridViewAdapter3");
        } catch (Exception e) {
        } finally {
            return convertView;
        }
    }

    @Override
    public int getCount() {
        if (_netAlbumInfos != null) {
            return _netAlbumInfos.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public void setGridviewSize(int w) {

    }
}
